<?php

namespace App\Repositories;

use App\Models\City;
use Illuminate\Support\Facades\Redis;

class CityRepository extends BaseRepository
{
    /**
     *  REDIS_EXPIRES_FOR_WEEKS 缓存有效期一周
     */
    const REDIS_EXPIRES_FOR_WEEKS = 604800;

    public function model()
    {
        return City::class;
    }

    /**
     * 获取市列表
     * Date: 2018/05/10
     * @author Tenny
     * @param $province_id
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     */
    public function getCity($province_id = false)
    {
        if ($province_id !== false) {
            $this->where('province_id', '=', $province_id);
        }
        $redis_key = sprintf('GET_CITY_LISTS_V1_%d', (int)$province_id);
        if (!Redis::exists($redis_key)) {
            $city = $this->get();
            Redis::setex($redis_key, self::REDIS_EXPIRES_FOR_WEEKS, json_encode($city));
        } else {
            $res = Redis::get($redis_key);
            $city = json_decode($res);
        }
        return $city;
    }

    protected function serialization(array $attributes)
    {
        // TODO: Implement serialization() method.
    }
}